#/bin/bash

IMAGE_NAME=ubuntu_dev:18.04
CONTAINER_NAME=ubuntu-devel

if [[ $1 == "-h" ]]; then
    echo "bash run_ubuntu.sh container_name"
    exit 0
fi

if [[ -n $1 ]]; then
    CONTAINER_NAME=$1
fi

##存在退出或者正在运行的container
EXIST_IMAGE=$(docker ps -a | grep $CONTAINER_NAME | awk '{print $2}')
EXIST_CONTAINER_HASH=$(docker ps -a | grep $CONTAINER_NAME | awk '{print $1}')

##正在运行的container
EXIST_RUNING_IMAGE=$(docker ps | grep $CONTAINER_NAME | awk '{print $2}')

if [[ $EXIST_RUNING_IMAGE == $IMAGE_NAME ]];then
    docker exec -ti "$EXIST_CONTAINER_HASH" bash
elif [[ $EXIST_IMAGE == $IMAGE_NAME ]]; then
    docker start -ai $EXIST_CONTAINER_HASH
else
    docker run --privileged --name $CONTAINER_NAME -v $(pwd):/root/workspace -w /root/workspace -ti $IMAGE_NAME  bash
fi
